package cn.zjh.org.controller;

import cn.zjh.base.utils.AjaxResult;
import cn.zjh.base.utils.PageList;
import cn.zjh.org.domain.Department;
import cn.zjh.org.query.DepartmentQuery;
import cn.zjh.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description: 部门表象层
* @Author: Mr.Zhang
* @Version: 1.0
* @Date:  2022/6/24 13:10
*/
@RestController
@RequestMapping("/department")
public class DepartmentController {


    @Autowired
    private IDepartmentService departmentService;


    /**
    * @Title: loadById
    * @Description: 根据id查询
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/24 13:15
    * @Parameters:
    * @Return
    */
    @GetMapping("/{id}")
    public Department loadById(@PathVariable("id") Long id) {
        return departmentService.loadById(id);
    }


    /**
    * @Title: loadById
    * @Description: 查询所有
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/24 13:18
    * @Parameters:
    * @Return
    */
    @GetMapping()
    public List<Department> loadAll() {
        return departmentService.loadAll();
    }

    /**
    * @Title: remove
    * @Description: 删除
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/24 13:22
    * @Parameters:
    * @Return
    */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，请重试！！！");
        }
    }


    /**
    * @Title: addOrUpt
    * @Description: 新增/修改
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/24 13:26
    * @Parameters:
    * @Return
    */

    @PutMapping()
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，请重试！！！");
        }
    }


    /**
    * @Title: remove
    * @Description: 高级查询+分页查询
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/24 13:28
    * @Parameters:
    * @Return
    */
    @PostMapping()
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery){
//        System.err.println(departmentQuery);
//        System.err.println(departmentQuery);
//        System.err.println(departmentQuery);
        PageList<Department> data = departmentService.queryData(departmentQuery);
        return data;
    }


    /**
    * @Title: batchDel
    * @Description: 批量删除
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/6/25 22:12
    * @Parameters:
    * @Return
    */
    @PatchMapping()
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，请重试！！！");
        }
    }

    @GetMapping("/tree")
    public List<Department> getTree() {
        return departmentService.getTree();
    }

}
